/*
 * Created on 25.06.2006 by leck
 *
 */
package de.devisnik.eidle.picture;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import de.devisnik.eidle.util.ScreenWithDuration;

public class PictureScreen extends ScreenWithDuration {

	private Canvas itsCanvas;
	private Image itsImage;
	private final ImageDescriptor descriptor;

	public PictureScreen(final ImageDescriptor descriptor, final int duration) {
		super("Picture", duration);
		Assert.isNotNull(descriptor);
		this.descriptor = descriptor;
	}

	public void createControl(final Composite parent) {
		super.createControl(parent);

		itsCanvas = new Canvas(parent, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE);
		itsCanvas.setBackground(itsCanvas.getDisplay().getSystemColor(
				SWT.COLOR_BLACK));
		itsCanvas.addListener(SWT.Resize, new Listener() {

			public void handleEvent(final Event e) {
				itsCanvas.redraw();
			}
		});
		itsCanvas.addListener(SWT.Paint, new Listener() {

			public void handleEvent(final Event e) {
				ImageUtils.drawImage(e.gc, itsImage, itsCanvas.getClientArea());
			}
		});

		itsImage = descriptor.createImage();
	}

	public void disposeControl() {
		super.disposeControl();
		itsCanvas.dispose();
		itsImage.dispose();
	}

	public void setFocus() {
		itsCanvas.setFocus();
	}

	public void dispose() {
		if (itsImage != null) {
			itsImage.dispose();
		}
	}

}
